import 'package:flutter/material.dart';
import 'package:flutter_web/controller/menu_controller.dart';
import 'package:get/get.dart';

class SliderMenuWidget extends StatefulWidget {
  const SliderMenuWidget({Key key}) : super(key: key);

  @override
  _SliderMenuWidgetState createState() => _SliderMenuWidgetState();
}

class _SliderMenuWidgetState extends State<SliderMenuWidget> {
  MenuController _menuController = Get.put(MenuController());
  @override
  Widget build(BuildContext context) {
    return Drawer(
        child: Container(
      color: Colors.black,
      child: Obx(() {
        return buildListView();
      }),
    ));
  }

  Widget buildListView() {
    return ListView(
      children: [
        DrawerHeader(
          child: Container(
            padding: EdgeInsets.only(top: 60),
            child: Text(
              "GGB-flutter-web探险之路",
              style: TextStyle(
                color: Colors.red,
                fontWeight: FontWeight.w500,
                fontSize: 16,
              ),
            ),
          ),
        ),
        ...List.generate(
          _menuController.menuList.length,
          (index) => SlideItemMenuWidget(_menuController.menuList[index],
              _menuController.currentSelectIndex == index, () {
            _menuController.updateSelectIndex(index);
          }),
        ),
      ],
    );
  }
}

class SlideItemMenuWidget extends StatelessWidget {
  final String title;
  final bool isSelect;
  final Function onTap;
  const SlideItemMenuWidget(this.title, this.isSelect, this.onTap);

  @override
  Widget build(BuildContext context) {
    return ListTile(
      onTap: onTap,
      contentPadding: EdgeInsets.only(left: 20),
      // 是否选中
      selected: isSelect,
      selectedTileColor: Colors.red,
      title: Text(
        title,
        style: TextStyle(
          color: Colors.white,
          fontWeight: FontWeight.w500,
          fontSize: 16,
        ),
      ),
    );
  }
}
